package com.pccw.usercentercontroller.api;

import com.pccw.usercentercontroller.converter.UserConverter;
import com.pccw.usercentercontroller.vo.BaseRespVO;
import com.pccw.usercentercontroller.vo.UserRegisterVO;
import com.pccw.usercentercontroller.vo.UserVO;
import com.pccw.usercenterservice.dto.SysUserDTO;
import com.pccw.usercenterservice.dto.UserRegisterDTO;
import com.pccw.usercenterservice.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户注册
     * @param userRegisterVO
     * @return
     */
    @RequestMapping(value="/register",method = RequestMethod.POST)
    public BaseRespVO<Object> register(@RequestBody @Validated UserRegisterVO userRegisterVO){
       userRegisterVO.checkValid();
        UserRegisterDTO userRegisterDTO = UserConverter.converteRegisteVo2DTO(userRegisterVO);
        Boolean register = sysUserService.register(userRegisterDTO);
        return BaseRespVO.success(register);
    }

    /**
     * 用户获取
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public BaseRespVO<UserVO> getUserById(@PathVariable String id){
        SysUserDTO byId = sysUserService.getById(id);
        return BaseRespVO.success(UserConverter.converteUserDto2Vo(byId));
    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}",method = RequestMethod.DELETE)
    public BaseRespVO<Integer> deleteById(@PathVariable String id){
        Integer integer = sysUserService.deleteById(id);
        return BaseRespVO.success(integer);
    }

    /**
     * 用户修改
     * @param userVO
     * @return
     */
    @RequestMapping(value="/update",method = RequestMethod.PUT)
    public BaseRespVO<Integer> update(@RequestBody UserVO userVO){
        SysUserDTO sysUserDTO = UserConverter.converteUserVO2Dto(userVO);
        Integer integer = sysUserService.updateById(sysUserDTO);
        return BaseRespVO.success(integer);
    }
}
